package com.jun.ribbon.service;

import com.jun.ribbon.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 描述:
 *
 * @outhor jun
 * @create 2018-04-23 14:46
 */
public class UserService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "defaultUser")
    @HystrixCollapser(batchMethod = "findAll" , collapserProperties = {@HystrixProperty(name = "timerDelayInMilliseconds",value = "100")})
    public User getUserById(Long id){
        return restTemplate.getForObject("http://USER-SERVICE/users/{1}",User.class,id);
    }

//    @HystrixCommand
//    public User getUserByIdAsynic(final String id){
//        return new AsyncResult<User>(){
//            @Override
//            public User invoke(){
//
//            }
//        };
//
//    }
    @HystrixCommand
    public List<User> findAll(List<Long> ids){
        return restTemplate.getForObject("http://USER-SERVICE/users/ids={1}" , List.class , StringUtils.join(ids , ","));
    }
}
